import { useParams } from 'common'
import { useRouter } from 'next/router'
import { useEffect, useMemo } from 'react'
import { Admonition } from 'ui-patterns'
import { PageContainer } from 'ui-patterns/PageContainer'
import {
PageHeader,
PageHeaderDescription,
PageHeaderMeta,
PageHeaderSummary,
PageHeaderTitle,
} from 'ui-patterns/PageHeader'
import { PageSection, PageSectionContent } from 'ui-patterns/PageSection'
import { GenericSkeletonLoader } from 'ui-patterns/ShimmeringLoader'
import { useAvailableIntegrations } from '@/components/interfaces/Integrations/Landing/useAvailableIntegrations'
import { useInstalledIntegrations } from '@/components/interfaces/Integrations/Landing/useInstalledIntegrations'
import { DefaultLayout } from '@/components/layouts/DefaultLayout'
import { ProjectIntegrationsLayoutDispatch } from '@/components/layouts/ProjectIntegrationsLayoutDispatch'
import type { NextPageWithLayout } from '@/types'
const IntegrationPage: NextPageWithLayout = () => {
const router = useRouter()
const { ref, id, pageId, childId } = useParams()
const { data: allIntegrations } = useAvailableIntegrations()
const { installedIntegrations: installedIntegrations, isLoading: isIntegrationsLoading } =
useInstalledIntegrations()
// everything is wrapped in useMemo to avoid UI resets when installing additional extensions like pg_net
const integration = useMemo(() => allIntegrations.find((i) => i.id === id), [allIntegrations, id])
const installation = useMemo(
() => installedIntegrations.find((inst) => inst.id === id),
[installedIntegrations, id]
)
// Get the corresponding component dynamically
const Component = useMemo(
() => integration?.navigate({ id, pageId, childId }),
[integration, id, pageId, childId]
)
useEffect(() => {
// if the integration is not installed, redirect to the overview page
if (
router &&
router?.isReady &&
!isIntegrationsLoading &&
!installation &&
pageId !== 'overview'
) {
router.replace(`/project/${ref}/integrations/${id}/overview`)
}
}, [installation, isIntegrationsLoading, pageId, router, ref, id])
// Determine content based on state
const content = useMemo(() => {
if (!router?.isReady || isIntegrationsLoading) {
return (
)
} else if (!Component || !id || !integration) {
return (
Integration not found
If you think this is an error, please contact support.
Please try again later or contact support if the problem persists.
)
} else {
return
}
}, [router?.isReady, isIntegrationsLoading, id, integration, Component])
return content
}
IntegrationPage.getLayout = (page) => (
{page}
)
export default IntegrationPage